package com.example.response;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

/**
 * 接口通用的返回对象。
 */
@NoArgsConstructor
@AllArgsConstructor
@Getter
@Setter
public final class R<T> {
    private int code;
    private String msg;
    private T data;

    public R(int code) {
        this.code = code;
        this.msg = "";
        this.data = null;
    }

    public R(int code, String msg) {
        this.code = code;
        this.msg = msg;
        this.data = null;
    }

    public static R success(Object data) {
        return new R(ResultCodeEnum.SUCCESS.getCode(), ResultCodeEnum.SUCCESS.getMessage(), data);
    }

    public static R success(String message, Object data) {
        return new R(ResultCodeEnum.SUCCESS.getCode(), message, data);
    }

    public static R success() {
        return success("");
    }


    public static R fail() {
        return fail("Failed");
    }
    public static R fail(String msg) {
        return new R(ResultCodeEnum.SYSTEM_EXCEPTION.getCode(), msg);
    }

    public static R fail(int code, String msg) {
        return new R(code, msg);
    }

    public boolean succeeded() {
        return getCode() == ResultCodeEnum.SUCCESS.getCode();
    }

    public boolean failed() {
        return getCode() != ResultCodeEnum.SUCCESS.getCode();
    }

}
